package dk.isten.andro.main;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.Toast;
import dk.isten.andro.*;

public class SuperActivity extends Activity {

	
	  private static boolean lockOrientation;
	  

	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		
		
		
		if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
	 	{
	 	 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
	 	}
	}

	// Called only once first time menu is clicked on
	  @Override
	  public boolean onCreateOptionsMenu(Menu menu)
	  { 
	    getMenuInflater().inflate(R.menu.menu, menu);
	    return true;
	  }
	  
	  // Called every time user clicks on a menu item
	  @Override
	  public boolean onOptionsItemSelected(MenuItem item)
	  { 
		  Toast.makeText(this, "in onOptionsItemSelected", Toast.LENGTH_SHORT);
		  switch (item.getItemId()) 
		  {
		  		case R.id.support:
		  			startActivity(new Intent(this, SupportActivity.class));
		  			break;
		  			
		  		case R.id.orientation:
		  			lockOrUnlockOrientation();
		  			break;
		  }  
		  
		  return true;
	  }
	  
	  private void lockOrUnlockOrientation() 
	  {
		  Log.d("kører superactivity", "måske eller");
		  if (lockOrientation == true)
		  {
			  Log.d("kører superactivity i true", "true");
			  lockOrientation = false;//if its lock we must unlock
			  setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
		  }
		  else
		  {
			  //else if its unlock we must lock because its a toggle button
			  lockOrientation = true;
			  if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
			  {
				  setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
			  }
			  else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)  
			  {
				  setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
			  }
		  }
	  }

	// Called every time menu is opened
	  @Override
	  public boolean onMenuOpened(int featureId, Menu menu)
	  { 
		  MenuItem toggleItem = menu.findItem(R.id.orientation); 
		  
		  if (lockOrientation == true)
		  { 
			  toggleItem.setTitle("unlock orientation");
			  toggleItem.setIcon(R.drawable.ic_jog_dial_unlock);
		  } else
		  { 
			  toggleItem.setTitle("lock orientation");
			  toggleItem.setIcon(R.drawable.ic_lock_lock);
		  }
		  return true;
	  }
	  public void setOrientation(Boolean bool){
		  if(bool==true){
			  lockOrientation = true;
			  setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

			  }
	  }


}//android.R.drawable.ic_lock_lock  
